const fetch = require('node-fetch');

async function testLogin() {
  try {
    // 登录获取token
    const loginResponse = await fetch('http://localhost:5000/api/auth/login/email', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        email: 'admin@aide.com',
        password: 'admin123456'
      })
    });

    const loginData = await loginResponse.json();
    console.log('Login response:', loginData);

    if (loginData.token) {
      // 测试聊天API
      const chatResponse = await fetch('http://localhost:5000/api/chat/enhanced-stream', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${loginData.token}`
        },
        body: JSON.stringify({
          messages: [{
            role: 'user',
            content: '你好'
          }],
          sessionId: 'test-session'
        })
      });

      console.log('Chat response status:', chatResponse.status);
      
      if (chatResponse.body) {
        const reader = chatResponse.body.getReader();
        const decoder = new TextDecoder();
        
        while (true) {
          const { value, done } = await reader.read();
          if (done) break;
          
          const chunk = decoder.decode(value);
          console.log('Chunk:', chunk);
        }
      }
    }
  } catch (error) {
    console.error('Test error:', error);
  }
}

testLogin();
